﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sherwood.SignOn.Server.Models.Services;
using Sherwood.SignOn.Server.Controllers;
using System.Web.Mvc;

namespace Sherwood.SignOn.Server.Tests.UserAccountTests
{
    [TestClass]
    public class DeleteUserAccountTests : TestBase
    {

        [TestMethod]
        public void DeleteUserAccountTest()
        {
            //Assemble
            IUserAccount user1 = TestHelper.RegisterValidDummyUser("NewUser", "someone@somewhere.sherwood.local", "John", "Brown");
            IUserAccount user2 = TestHelper.RegisterValidDummyUser("AnotherUser", "user2@somewhereelse.sherwood.local", "Joe", "Bloggs");
            IUserAccount user3 = TestHelper.RegisterValidDummyUser("YetAnother", "humpty@somewhereelse.sherwood.local", "Humpty", "van Dumpty");
            
            //Act
            Guid account2Id = user2.Id;
            bool retVal = Data.UserAccounts.DeleteUserAccount(user2.Id);

            //Assert
            Assert.IsTrue(retVal);
            Assert.IsTrue(Data.UserAccounts.GetAllAccounts().Count() == 2, "Account should have been deleted.");
            Assert.IsTrue(Data.UserAccounts.GetUserAccount(account2Id) == null, "Account should have been deleted.");

        }


        [TestMethod]
        public void DeleteNonExistingUserAccountTest()
        {
            //Assemble
            IUserAccount user1 = TestHelper.RegisterValidDummyUser("NewUser", "someone@somewhere.sherwood.local", "John", "Brown");
            IUserAccount user2 = TestHelper.RegisterValidDummyUser("AnotherUser", "user2@somewhereelse.sherwood.local", "Joe", "Bloggs");
            IUserAccount user3 = TestHelper.RegisterValidDummyUser("YetAnother", "humpty@somewhereelse.sherwood.local", "Humpty", "van Dumpty");

            //Act
            bool retVal = Data.UserAccounts.DeleteUserAccount(Guid.NewGuid());

            //Assert
            Assert.IsFalse(retVal);
            Assert.IsTrue(Data.UserAccounts.GetAllAccounts().Count() == 3, "No accounts should have been deleted.");

        }

        [TestMethod]
        public void DeleteUserAccountUsingController()
        {
            //Assemble
            IUserAccount user1 = TestHelper.RegisterValidDummyUser("NewUser", "someone@somewhere.sherwood.local", "John", "Brown");
            IUserAccount user2 = TestHelper.RegisterValidDummyUser("AnotherUser", "user2@somewhereelse.sherwood.local", "Joe", "Bloggs");
            IUserAccount user3 = TestHelper.RegisterValidDummyUser("YetAnother", "humpty@somewhereelse.sherwood.local", "Humpty", "van Dumpty");

            //Act
            AdministrationController controller = new AdministrationController();
            Guid account2Id = user2.Id;
            ContentResult result = controller.Delete(user2.Id) as ContentResult;

            //Assert
            Assert.IsTrue(result != null);
            Assert.IsTrue(Data.UserAccounts.GetAllAccounts().Count() == 2, "Account should have been deleted.");
            Assert.IsTrue(Data.UserAccounts.GetUserAccount(account2Id) == null, "Account should have been deleted.");
        }

        [TestMethod]
        public void DeleteNonExistingUserAccountUsingController()
        {
            //Assemble
            IUserAccount user1 = TestHelper.RegisterValidDummyUser("NewUser", "someone@somewhere.sherwood.local", "John", "Brown");
            IUserAccount user2 = TestHelper.RegisterValidDummyUser("AnotherUser", "user2@somewhereelse.sherwood.local", "Joe", "Bloggs");
            IUserAccount user3 = TestHelper.RegisterValidDummyUser("YetAnother", "humpty@somewhereelse.sherwood.local", "Humpty", "van Dumpty");

            //Act
            AdministrationController controller = new AdministrationController();

            try
            {
                ContentResult result = controller.Delete(Guid.NewGuid()) as ContentResult;
            }
            catch (Exception ex) // expect controller to throw an exception,...
            {

            }

            //Assert
            Assert.IsTrue(Data.UserAccounts.GetAllAccounts().Count() == 3, "Account should not have been deleted.");
        }
    }
}
